//package net.lab1024.sa.base.config;
//
//
//import org.apache.http.HttpHost;
//import org.apache.http.auth.AuthScope;
//import org.apache.http.auth.UsernamePasswordCredentials;
//import org.apache.http.conn.ssl.NoopHostnameVerifier;
//import org.apache.http.impl.client.BasicCredentialsProvider;
//import org.opensearch.client.RestClient;
//import org.opensearch.client.RestHighLevelClient;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//@Configuration
//public class OpenSearchConfig {
//
//    @Value("${spring.elasticsearch.uris:192.168.43.3:9200}")
//    private String opensearchUri;
//
//    @Value("${spring.elasticsearch.username:}")
//    private String username;
//
//    @Value("${spring.elasticsearch.password:}")
//    private String password;
//
//
////    @Bean
////    public RestHighLevelClient openSearchClient() {
////        return new RestHighLevelClient(
////                RestClient.builder(
////                        new HttpHost("localhost", 9200, "http")
////                )
////        );
////    }
//
//    @Bean(value = "RestHighLevelClient", destroyMethod = "close")
//    public RestHighLevelClient restHighLevelClient(){
//        //解析hostlist配置信息
//        String[] split = opensearchUri.split(",");
//        //创建HttpHost数组，其中存放es主机和端口的配置信息
//        HttpHost[] httpHostArray = new HttpHost[split.length];
//        for(int i=0;i<split.length;i++){
//            String item = split[i];
//            httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http");
//        }
//        //创建RestHighLevelClient客户端
//        return new RestHighLevelClient(RestClient.builder(httpHostArray).setHttpClientConfigCallback(httpClientBuilder -> {
//
//            httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
//            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//            credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username, password));
//            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//            return httpClientBuilder;
//        }));
//
//    }
//
//
////安全认证
////    @Bean
////    public RestHighLevelClient openSearchClient() {
////        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
////        credentialsProvider.setCredentials(
////                AuthScope.ANY,
////                new UsernamePasswordCredentials("admin", "admin-password")
////        );
////
////        return new RestHighLevelClient(
////                RestClient.builder(new HttpHost("localhost", 9200, "http"))
////                        .setHttpClientConfigCallback(httpClientBuilder ->
////                                httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
////                        )
////        );
////    }
//
////连接池 模式
////    @Bean
////    public RestHighLevelClient openSearchClient() {
////        return new RestHighLevelClient(
////                RestClient.builder(new HttpHost("localhost", 9200, "http"))
////                        .setHttpClientConfigCallback(httpClientBuilder -> {
////                            // 配置连接池
////                            httpClientBuilder.setMaxConnTotal(20);
////                            httpClientBuilder.setMaxConnPerRoute(10);
////                            return httpClientBuilder;
////                        })
////        );
////    }
//}